package com.my;

import com.my.mapper.RightColumnMapper;
import com.my.mapper.RightMapper;
import com.my.pojo.Right;
import com.my.pojo.RightColumn;
import com.my.util.MYBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/right")
public class RightController extends HttpServlet {
@Deprecated
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws SecurityException, IOException{
    req.setCharacterEncoding("utf-8");
    HttpSession session = req.getSession();
    resp.setContentType("text/html;charset=utf-8");
    String action = req.getParameter("action");
    String result = "";
    SqlSession sqlSession = MYBatisUtil.openSession();
    if(action.equals("preAdd")){
        RightColumnMapper rightColumnMapper = sqlSession.getMapper(RightColumnMapper.class);
        List<RightColumn> rightColumnList = rightColumnMapper.searcherAllRightColumn();
        if(rightColumnList == null) {
            result="error.jsp";
        }else{
            session.setAttribute("allRightColumn",rightColumnList);
            result="right/addRight.jsp";
        }
    }
    if(action.equals("add")){
        String rightColumnIdStr = req.getParameter("rightCp;umnId");
        String rightName = req.getParameter("rightName");
        String rightDesc = req.getParameter("rightDesc");
        String url = req.getParameter("url");
        Right right = Right.builder().rightId(Integer.parseInt(rightColumnIdStr)).rightName(rightName).rightDesc(rightDesc).url(url).build();
        RightMapper rightMapper = sqlSession.getMapper(RightMapper.class);
        Boolean existRight = rightMapper.isExistRight(rightName);
        if(existRight ==null){
            rightMapper.addRinght(right);
            sqlSession.clearCache();
            List<Right> list = this.searchAllRightColumnAndRight();
            if(list ==null){
                String retult = "error.jsp";
            }else{
                session.setAttribute("allRight",list);
                result = "right/showAllRight.jsp";
            }
        }else{
            result = "errpr.jsp";
        }
    }
    if (action.equals("showAllRight")){
        List<Right> list = this.searchAllRightColumnAndRight();
        if(list ==null) {
            result = "error.jsp";
        }else {
            session.setAttribute("allRight",list);
            result = "right/showAllRight.jsp";
        }
        resp.sendRedirect(result);
    }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws SecurityException, IOException, ServletException {
        this.doGet(req, resp);
    }

    public List searchAllRightColumnAndRight() {
        SqlSession sqlSession = MYBatisUtil.openSession();
        RightColumnMapper rightColumnMapper = sqlSession.getMapper(RightColumnMapper.class);
        List<RightColumn> rightColumnList = rightColumnMapper.searcherAllRightColumn();
        RightMapper rightMapper = sqlSession.getMapper(RightMapper.class);
        List list3 = new ArrayList();
        for (int i = 0; i<rightColumnList.size(); i++) {
            RightColumn rightColumn = rightColumnList.get(i);
            String rightColumnName = rightColumn.getRightColumnName();
            List<Right> rightList = rightMapper.searchAllrightByRightColumnId(rightColumn.getId());
            List list1 = new ArrayList();
            List list2 = new ArrayList();
            list1.add(rightColumnName);
            list2.add(list1);
            list3.add(list2);
        }
        return list3;
    }
}
